package metrics;

import java.awt.Color;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.xy.XYSeriesCollection;

public class PrRcGraph extends ApplicationFrame {

    public PrRcGraph(Vector<NormalizedPRStatictic> entries) {

        super("Precision Recall Graph");

        final XYDataset dataset = createDataset(entries);
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 350));
        setContentPane(chartPanel);

    }

	private XYDataset createDataset(Vector<NormalizedPRStatictic> summary) {

        final XYSeriesCollection dataset = new XYSeriesCollection();
        for(int j = 0; j < summary.size(); j++) {
        
            final XYSeries series = new XYSeries(summary.get(j).getLabel());

            for (int i = 0; i <= NormalizedPRStatictic.NORMALIZAION_PRECISION; i++) {
                float x = (float) i*(float)1/NormalizedPRStatictic.NORMALIZAION_PRECISION;
                float y = (float) summary.get(j).getPrecisionOfEntry(i);
                series.add(x, y);
   
            }
            dataset.addSeries(series);

        }

        return dataset;
        
    }
    
    private JFreeChart createChart(final XYDataset dataset) {
        
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Precision Recall Graph",      
            "Recall",                      
            "Precision",                     
            dataset,                 
            PlotOrientation.VERTICAL,
            true,                     
            true,                     
            false                     
        );

        chart.setBackgroundPaint(Color.white);

        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        //renderer.setSeriesLinesVisible(1, false);
        //renderer.setSeriesShapesVisible(1, false);

        plot.setRenderer(renderer);
                
        return chart;
        
    }
}
